package 多线程进阶;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch的使用
 */
public class TestDemo5 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread thread=new Thread(()->{
                try {
                    System.out.println("线程正在等待中");
                    Thread.sleep((long) (Math.random()*10000));
                    System.out.println("线程等待完毕");
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
        countDownLatch.await();
        System.out.println("执行结束");
    }
}
